#!/usr/bin/evn python
# -*- coding: utf-8 -*-
from typing import Optional

from fastapi import FastAPI
from pydantic import field_validator
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    debug: bool = False
    title: str
    description: str
    version: str

    class Config:
        env_file = ".env"
        env_file_encoding = 'utf-8'

    @classmethod
    @field_validator('version', mode='before')
    def version_len_check(cls, v: str) -> Optional[str]:
        if v and len(v) == 0:
            return None
        return v

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


settings = Settings(_env_file='.env')
app = FastAPI(
    debug=settings.debug,
    title=settings.title,
    description=settings.description,
    version=settings.version,
)

if __name__ == "__main__":
    import uvicorn
    import os

    app_model_name = os.path.basename(__file__).replace(".py", "")
    print(app_model_name)
    uvicorn.run(f"{app_model_name}:app", host='127.0.0.1')
